<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/14 15:41
 */

namespace App\Http\Models;


class UserIntegralLog extends BaseModel
{
    protected $table = 'users_integral_log';
    protected $guarded = [];

    public function user()
    {
        return $this->hasOne('App\Http\Models\User', 'id', 'userid');
    }

    /**
     * 增加用户扣分
     * @param $userid
     * @param $content
     * @param $number
     * @param $goodsid
     * @param status  0扣款,1充值
     * @return bool
     */
    public static function addUserIntegralLog($userid, $content, $number, $goodsid, $status = 0, $refuse = '')
    {
        $userCapital = UserCapital::where(['user_id' => $userid])->first();
        if ($status == 1 && $number == env('UserIntegral')) {
            $balance = 20;
        } else {
            $balance = $userCapital->integral;
        }
        $data = [
            'user_id' => $userid,
            'content' => $content,
            'number' => $number,
            'good_id' => $goodsid,
            'status' => $status,
            'refuse' => $refuse,
            'balance' => $balance
        ];
        $log_bool = self::create($data);
        return $log_bool;
    }
}